package com.example.pinkcat.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.pinkcat.R;
import com.example.pinkcat.activity.api.getData.CopyFileByUri;
import com.example.pinkcat.activity.api.getData.GetData;
import com.rishabhharit.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MakeVideoPage extends AppCompatActivity {
    Context context=this;

    Spinner video_type_select;//视频类型
    List<String> list = new ArrayList<String>();

    RoundedImageView videoface;//视频封面
    TextView changevideo;//选择视频
    TextView changeface;//选择封面

    TextView editTitle;//标题
    String video_type="科技";
    TextView editvideolabs;//标签
    TextView videointroduction;//视频介绍
    String videopath;
    String videofacepath;

    TextView garement;//协议
    TextView sendvideo;//视频上传按钮

    ProgressBar progressBar_wait;

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Objects.requireNonNull(getSupportActionBar()).hide();//去掉标题栏
        //        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.activity_make_video_page);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }//深色信息栏

        video_type_select=(Spinner) findViewById(R.id.vieo_type_select);
        videoface=findViewById(R.id.videoface);
        changevideo=findViewById(R.id.changevideo);
        changeface=findViewById(R.id.changeface);
        editTitle=findViewById(R.id.input_tile);
        editvideolabs=findViewById(R.id.input_video_tage);
        videointroduction=findViewById(R.id.input_videointroduction);
        garement=findViewById(R.id.vieosendtxt);
        sendvideo=findViewById(R.id.sendbtn);
        progressBar_wait=findViewById(R.id.progressBar_wait);

        progressBar_wait.setProgress(View.INVISIBLE);



        listen();
        setSpanner();

    }

    void listen(){
        //选择视频
        changevideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startVideo();
            }
        });
        //选择封面
        changeface.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startPhoto();
            }
        });
        //上传视频
        sendvideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                progressBar_wait.setVisibility(View.VISIBLE);
                sendvideo.setEnabled(false);
                if (videopath==null||videofacepath==null||editTitle.getText().toString().length()<1||editvideolabs.getText().toString().length()<1||videointroduction.getText().toString().length()<1){
                    Toast.makeText(context,"请完善视频信息再上传",Toast.LENGTH_SHORT).show();
                    progressBar_wait.setVisibility(View.INVISIBLE);
                    sendvideo.setEnabled(true);
                    return;
                }else {
                    if (GetData.sendVideo(videopath,videofacepath,editTitle.getText().toString(),video_type,editvideolabs.getText().toString(),videointroduction.getText().toString(),context)){
                        progressBar_wait.setVisibility(View.INVISIBLE);
                        onBackPressed();
                    }
                }
                sendvideo.setEnabled(true);
                progressBar_wait.setVisibility(View.INVISIBLE);
            }
        });
        //下拉框选择监听
        video_type_select.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                video_type=list.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //返回监听
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        //协议跳转
        garement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, PinCatAgreement.class);
                intent.putExtra("agreementTitle","用户协议");
                startActivity(intent);
            }
        });
    }
    void setSpanner(){
        list.add("科技");
        list.add("体育");
        list.add("美食");
        list.add("舞蹈");
        list.add("游戏");
        list.add("汽车");
        list.add("音乐");
        list.add("生活");
        list.add("娱乐");
        ArrayAdapter<String> spinner_adapter=new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item,list);
        spinner_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        video_type_select.setAdapter(spinner_adapter);
    }

    //打开相册或文件选取图片
    void startPhoto(){
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent, 2);
    }
    //打开相册或文件选取视频
    void startVideo(){
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "video/*");
        startActivityForResult(intent, 3);
    }

    //返回并获取文件地址
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2) {
            // 从相册返回的数据
            //Log.e(this.getClass().getName(), "Result:" + data.toString());
            if (data != null) {
                // 得到图片的全路径
                Uri uri = data.getData();
                Glide.with(context).load(uri).into(videoface);
                videofacepath= CopyFileByUri.getPath(context,uri,"image.png");
            }
        }else if ((requestCode == 3)){
            // 从相册返回的数据
            //Log.e(this.getClass().getName(), "Result:" + data.toString());
            if (data != null) {
                // 得到图片的全路径
                Uri uri = data.getData();
                videopath= CopyFileByUri.getPath(context,uri,"video.mp4");
            }
        }
    }
}